package com.epfly.promo.activity.impl.discount.predicate;

import com.epfly.iface.promo.activity.enums.ActivityObjectTypeCascadeEnum;
import com.epfly.iface.promo.activity.enums.ActivityObjectTypeEnum;
import com.epfly.promo.activity.dao.ActivityObjectMapper;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.entity.ActivityObjectEntity;
import com.epfly.promo.activity.feign.DataRestaurantFeignService;
import com.epfly.promo.activity.feign.UserFeignService;
import com.epfly.promo.activity.impl.ActivityUtil;
import com.epfly.promo.activity.impl.discount.ActivityWrapper;
import com.ifood.model.merchants.param.response.IdentityEmpResp;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 客户群体过滤
 */
@Order(value = PredicateOrder.ACTIVITY_OBJECT_TYPE_ORDER)
@Service
public class ActivityObjectTypePredicate  implements  ActivityDiscountPredicate {

    @Resource
    private ActivityObjectMapper activityObjectMapper;

    @Resource
    private DataRestaurantFeignService dataRestaurantFeignService;

    @Resource
    private UserFeignService userFeignService;


    @Override
    public boolean test(ActivityWrapper activityWrapper) {

        ActivityEntity activityEntity = activityWrapper.getActivityEntity();
        // 客户群体选择了"不限"
        if (activityEntity.getActivityObjectType().equals(ActivityUtil.join(ActivityObjectTypeEnum.UN_LIMIT.getKey()))) {
            return true;
        }

        String userID = activityWrapper.getUserID();

        Boolean isMember = userFeignService.isMember(userID).getIsMember();
        IdentityEmpResp identity = dataRestaurantFeignService.getIdentity(userID);
        Boolean isEmployee = identity.getIsEmployee();

        // 既非会员，亦非员工，即是散户
        boolean isConsumer = !isMember && !isEmployee;

        // 用户身份是散户，客户群体包含散户，可享受优惠活动。
        if (isConsumer && activityEntity.getActivityObjectType().contains(ActivityUtil.join(ActivityObjectTypeEnum.CONSUMER.getKey()))) {
            return true;
        }

        // 用户身份是会员，客户群体包含会员，可享受优惠活动。
        if (isMember && activityEntity.getActivityObjectType().contains(ActivityUtil.join(ActivityObjectTypeEnum.MEMBER.getKey()))) {
            return true;
        }

        // 用户是员工身份
        if (isEmployee && activityEntity.getActivityObjectType().contains(ActivityUtil.join(ActivityObjectTypeEnum.EMPLOYEE.getKey()))) {
            Byte activityObjectTypeCascade = activityEntity.getActivityObjectTypeCascade();
            // "部门/职务"选择了"不限"
            if (activityObjectTypeCascade == ActivityObjectTypeCascadeEnum.NO_LIMIT.getKey()) {
                return true;
            }

            String deptId = identity.getDepartmentId();
            // "部门/职务"选择了"指定部门"
            if (activityObjectTypeCascade == ActivityObjectTypeCascadeEnum.ASSIGN_DEPT.getKey()) {
                ActivityObjectEntity objectEntity = activityObjectMapper.findByActivityIdAndRelatedObjectId(activityEntity.getId(), deptId);
                if (Objects.nonNull(objectEntity)) {
                    return true;
                }
            }

            String jobId = identity.getPositionId();
            // "部门/职务"选择了"指定职务"
            if (activityObjectTypeCascade == ActivityObjectTypeCascadeEnum.ASSIGN_JOB.getKey()) {
                ActivityObjectEntity objectEntity = activityObjectMapper.findByActivityIdAndRelatedObjectId(activityEntity.getId(), jobId);
                if (Objects.nonNull(objectEntity)) {
                    return true;
                }
            }
        }

        return false;
    }
}
